import org.w3c.dom.Node;

import java.util.*;

public class Solution {
    public static class Node {
        public int val;
        public List<Node> children;

        public Node() {}

        public Node(int _val) {
                val = _val;
            }

        public Node(int _val, List<Node> _children) {
                val = _val;
                children = _children;
            }
    }

    /**
     * N 叉树的层序遍历
     * @param root
     * @return
     */
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> res = new ArrayList<>();
        if (null == root) {
            return res;
        }
        // 用于存放节点
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        int n = queue.size();
        while (!queue.isEmpty()) {
            n = queue.size();
            List<Integer> list = new ArrayList<>();
            while (n > 0) {
                Node node = queue.poll();
                list.add(node.val);
                // 将子节点加入队列中
                for (Node child: node.children) {
                    queue.add(child);
                }
                n--;
            }
            res.add(list);
        }
        return res;
    }

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        public TreeNode() {}
    }

    /**
     * 二叉树的锯齿形层序遍历
     * @param root
     * @return
     */
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if (null == root) {
            return res;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int n = 0;
        int k = 1;
        while(!queue.isEmpty()) {
            n = queue.size();
            List<Integer> list = new ArrayList<>();
            while(n > 0) {
                TreeNode node = queue.poll();
                list.add(node.val);
                if (null != node.left) {
                    queue.add(node.left);
                }
                if (null != node.right) {
                    queue.add(node.right);
                }
                n--;
            }
            if (k == -1) {
                Collections.reverse(list);
            }
            k = -k;
            res.add(list);
        }
        return res;
    }

    public static class Pair<T, U> {
        private T key;
        private U value;

        public T getKey() {
            return key;
        }

        public U getValue() {
            return value;
        }

        public Pair (T key, U value) {
            this.key = key;
            this.value = value;
        }
    }

    /**
     * 二叉树最大宽度
     * 利用数组存储二叉树的方式，给结点编号
     * @param root
     * @return
     */
    public int widthOfBinaryTree(TreeNode root) {
        if (null == root) {
            return 0;
        }
        // 队列中存储节点 + 下标
        Queue<Pair<TreeNode, Integer>> queue = new LinkedList<>();
        queue.add(new Pair<TreeNode, Integer>(root, 1));
        int n = 0, max = 0;;
        while (!queue.isEmpty()) {
            int left = queue.peek().getValue(), right = 0;
            // 入队
            n = queue.size();
            while (n > 0) {
                Pair<TreeNode, Integer> pair = queue.poll();
                int index = pair.getValue();
                TreeNode node = pair.getKey();
                if (null != node.left) {
                    queue.add(new Pair<TreeNode, Integer>(node.left, 2 * index));
                }
                if (null != node.right) {
                    queue.add(new Pair<TreeNode, Integer>(node.right, 2 * index + 1));
                }
                if (n == 1) {
                    right = index;
                }
                n--;
            }
            max = Math.max(max, right - left + 1);
        }
        return max;
    }

    /**
     * 在每个树行中找最大值
     * @param root
     * @return
     */
    public List<Integer> largestValues(TreeNode root) {
        List<Integer> res = new ArrayList<>();
        if (null == root) {
            return res;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        int n = 0;
        while(!queue.isEmpty()) {
            n = queue.size();
            int max = queue.peek().val;
            while(n > 0) {
                TreeNode node = queue.poll();
                max = Math.max(max, node.val);
                if (null != node.left) {
                    queue.add(node.left);
                }
                if (null != node.right) {
                    queue.add(node.right);
                }
                n--;
            }
            res.add(max);
        }
        return res;
    }
}
